package vip.specimen.system.service;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.datasource.entity.SystemAdvertisingEntity;
import vip.specimen.system.datasource.entity.SystemUserEntity;
import vip.specimen.system.repository.SystemAdvertisingRepository;
import vip.specimen.system.datasource.validation.SystemAdvertisingValidation;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/03/07 15:06
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Slf4j
@Service
public class SystemAdvertisingService {
    @Resource
    SystemAdvertisingRepository advertisingRepository;
    @Resource
    private SystemUserService systemUserService;
    @Resource
    private HttpServletRequest request;

    /**
     * 新增广告
     *
     * @param ad 广告体
     * @return 广告
     */
    @Transactional
    public SystemAdvertisingEntity insert(SystemAdvertisingValidation ad) {
        SystemUserEntity current_user = (SystemUserEntity) SecurityUtils.getSubject().getPrincipal();
        return this.advertisingRepository.save(new SystemAdvertisingEntity(ad.getAdvertisingName(), this.systemUserService.findByUuid(current_user.getUuid()), Boolean.FALSE, ad.getEndTime(), ad.getAdvertisingContent(), CommonUtil.stringToMd5(UUID.randomUUID().toString())));
    }

    /**
     * 获取列数据
     *
     * @return 数据集
     */
    public Page<SystemAdvertisingEntity> findAll() {
        JSONObject param = CommonUtil.pageParams(this.request);
        Pageable pageable = DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime"));
        return this.advertisingRepository.findAll(pageable);
    }


    @Transactional
    public SystemAdvertisingEntity update(UUID uuid) {
        try {
            if (this.advertisingRepository.existsById(uuid)) {
                SystemAdvertisingEntity ad = this.advertisingRepository.findById(uuid).get();
                ad.setEnable(!ad.isEnable());
                ad.setUpdateTime(Calendar.getInstance().getTime());
                return this.advertisingRepository.save(ad);
            }
        } catch (Exception e) {
            log.error("更新广告异常:" + e.getMessage());
        }
        return null;
    }

    /**
     * 根据uuid删除
     *
     * @param uuid uuid
     * @return success or failed
     */
    @Transactional
    public boolean delete(UUID uuid) {
        try {
            if (this.advertisingRepository.existsById(uuid)) {
                this.advertisingRepository.deleteById(uuid);
                return true;
            }
        } catch (Exception e) {
            log.error("删除广告异常:" + e.getMessage());
        }
        return false;
    }

    /**
     * 根据uuid List 删除
     *
     * @param uuids List -> uuid
     * @return successNum 成功数
     */
    @Transactional
    public int delete(Set<UUID> uuids) {
        int successNum = 0;
        for (UUID uuid : uuids) {
            successNum += this.delete(uuid) ? 1 : 0;
        }
        return successNum;
    }

    /**
     * 根据uuid 更新广告
     *
     * @param uuid uuid
     * @param ad_v 广告验证对象
     * @return this_obj
     */
    @Transactional
    public SystemAdvertisingEntity update(UUID uuid, SystemAdvertisingValidation ad_v) {
        try {
            if (this.advertisingRepository.existsById(uuid)) {
                SystemAdvertisingEntity ad = this.advertisingRepository.findById(uuid).get();
                ad.setAdvertisingContent(ad_v.getAdvertisingContent());
                ad.setAdvertisingName(ad_v.getAdvertisingName());
                ad.setEndTime(ad.getEndTime());
                ad.setUpdateTime(Calendar.getInstance().getTime());
                this.advertisingRepository.save(ad);
                return ad;
            }
        } catch (Exception e) {
            log.error("更新广告异常:" + e.getMessage());
        }
        return null;
    }

    /**
     * 统计
     *
     * @return 数目
     */
    public long count() {
        return this.advertisingRepository.count();
    }
}
